// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    discover: [{
      id: 1,
      text: "IT及其他"
    },
    {
      id: 2,
      text: "消防"
    },
    {
      id: 3,
      text: "数据中心/机房"
    },
    {
      id: 4,
      text: "音视频"
    },
    {
      id: 5,
      text: "设备管理"
    },
    {
      id: 6,
      text: "信息设施"
    },
    {
      id: 7,
      text: "公共安全"
    },
    {
      id: 8,
      text: "建筑智能化综合"
    },
    {
      id: 9,
      text: "系统调试"
    },
    {
      id: 10,
      text: "项目造价"
    },
    {
      id: 11,
      text: "项目设计"
    },
    {
      id: 12,
      text: "项目管理"
    },
    {
      id: 13,
      text: "工程安装"
    }
    ],
    searchContent: "",
    searchList: [],
    id: 0,
    name: "",
    companyName: ""
  },
  // 获取搜索框的内容
  searchContent: function (e) {
    this.setData({
      searchContent: e.detail.value
    }),
      console.log(this.data.searchContent)
  },
  // // 失去焦点清除input框内容
  onBindInputBlur: function (e) {
    e.detail.value = ""
  },
  // 搜索按钮
  search: function (e) {
    var that = this;
    if (this.data.searchContent == "") {
      wx.showToast({
        title: '请输入搜索内容',
        icon: "none"
      })
    } else {
      console.log(that.data.searchList);
      // that.setData({
      //   searchList: that.data.searchList
      // });
      //  名称相同的移除
      // for (var i = 0; i < that.data.searchList.length; i++) {
      //   for (var j = 0; j < that.data.searchList.length; j++) {
      //     if (i != j && that.data.searchList[i] == that.data.searchList[j]) {
      //       that.data.searchList.splice(j, 1);
      //       that.setData({
      //         searchList: that.data.searchList
      //       })
      //     }
      //   }

      // }
      // console.log(that.orderList)
     
      that.data.searchList.unshift(that.data.searchContent);
      that.unique(that.data.searchList);
      console.log(that.data.searchList)
      that.setData({
        searchList: that.data.searchList
      })
      // 超过20位移除一位
      if (that.data.searchList.length > 20) {
        var num = that.data.searchList.pop()
        that.setData({
          searchList: num
        })
        console.log(that.data.searchList)
      };
     
      // for (var i = 0; i < that.data.searchList.length; i++) {
      //   for (var j = 0; j < that.data.searchList.length; j++) {
      //     if (i != j && that.data.searchList[i] == that.data.searchList[j]) {
      //       that.data.searchList.splice(1, i);
      //       j--;
      //       that.setData({
      //         searchList: that.data.searchList
      //       })
      //     }
      //   }
      // }
      // 超过20位移除一位
      // if (this.data.searchList.length>20){
      //   var num = that.data.searchList.splice(1,19)
      //   that.setData({

      //     searchList: num
      //   })
      //   console.log(this.data.searchList)
      // };
     
      // that.setData({
      //   searchList: that.data.searchList
      // })
      console.log(that.data.searchList)
      wx.setStorageSync("historyStore", that.data.searchList);
      wx.navigateTo({
        url: '../masterResult/masterResult?name=' + that.data.searchContent,
      })
    }
  },
  unique:function(arr){
    for(var i = 0; i<arr.length; i++){
  for (var j = i + 1; j < arr.length; j++) {
    if (arr[i] == arr[j]) {         //第一个等同于第二个，splice方法删除第二个
      arr.splice(j, 1);
      j--;
    }
  }
}
return arr;
console.log(arr)
},
  clear: function () {
    this.setData({
      searchList: []
    })
    wx.setStorageSync("historyStore", this.data.searchList)
    // wx.clearStorageSync("historyStore")
  },
  history: function (e) {
    var that = this;
    var item = e.currentTarget.dataset.item;
    that.setData({
      searchContent: item
    })
    wx.navigateTo({
      url: '../masterResult/masterResult?name=' + that.data.searchContent,
    })
  },
  push: function (e) {
    var that = this;
    // var text = e.currentTarget.dataset.text;
    var name = e.currentTarget.dataset.name;
    // console.log(text);
    this.setData({
      name: name,
      searchContent: name
    })
    this.search();
   
    // wx.navigateTo({
    //   url: '../masterResult/masterResult?name=' + that.data.name,
    // })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    wx.request({
      url: 'https://www.xiaozheng8.com/serviceCate/findSmallCate',
      data: {},
      method: "post",
      success: function (res) {
        console.log(res);
        that.setData({
          discover: res.data.data
        })
        console.log(that.data.discover);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if (wx.getStorageSync("historyStore") == "") {
      return false;
    } else {
      var num = wx.getStorageSync("historyStore");
      this.setData({
        searchList: num
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      searchContent: []
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})